greedy implementation *1500

Please click on ads to support us..

Python Code:

t = int(input())

while t > 0:
    t = t - 1
    n = int(input())
    a = list(map(int, input().split()))

    cnt = {}
    vals = []
    for i in a:
        if i in cnt:
            cnt[i] = cnt[i] + 1
        else:
            vals.append(i)
            cnt[i] = 1

    gold = cnt[vals[0]]
    silver = 0
    p = 1
    while silver <= gold and p < len(vals):
        silver += cnt[vals[p]]
        p += 1

    bronze = 0
    while bronze <= gold and p < len(vals):
        bronze += cnt[vals[p]]
        p += 1
    while p < len(vals) and 2 * (gold + silver + bronze + cnt[vals[p]]) <= n:
        bronze += cnt[vals[p]]
        p += 1
    if 2 * (silver + bronze + gold) > n or gold >= silver or gold >= bronze:
        print("0 0 0")
        continue

    print(gold, silver, bronze)


Comments

Submit
0 Comments
More Questions

361A - Levko and Table
5E - Bindian Signalizing
687A - NP-Hard Problem
1542C - Strange Function
961E - Tufurama
129D - String
888A - Local Extrema
722B - Verse Pattern
278A - Circle Line
940A - Points on the line
1742C - Stripes
1742F - Smaller
1742B - Increasing
1742A - Sum
1742D - Coprime
390A - Inna and Alarm Clock
1398B - Substring Removal Game
1742G - Orray
416B - Art Union
962A - Equator
803B - Distances to Zero
291A - Spyke Talks
1742E - Scuza
1506D - Epic Transformation
1354G - Find a Gift
1426F - Number of Subsequences
1146B - Hate "A"
1718C - Tonya and Burenka-179
834A - The Useless Toy
1407D - Discrete Centrifugal Jumps